/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hancadsa.client.view.widget;

import br.com.sales.generics.client.log.LogBrowser;
import br.com.sales.generics.client.request.HannaRequestCallback;
import br.com.sales.generics.client.utils.NumbersUtils;
import br.com.sales.hancadsa.client.presenter.HanCadSAPresenter;
import br.com.sales.hancadsa.client.presenter.bean.ProductBean;
import br.com.sales.hancadso.client.service.HanCadSOService;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.Response;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author v
 */
public class NewRequestDialog extends PopupPanel {

    private static final NewRequestDialogUiBinder uiBinder = GWT.create(NewRequestDialogUiBinder.class);
    @UiField
    IntegerBox countEdit;
    @UiField
    Label minStock;
    @Inject
    private HanCadSOService hanCadSOService;
    private ProductBean bean;

    @Inject
    public NewRequestDialog() {
        super(false, true);
        add(uiBinder.createAndBindUi(this));
    }

    @Override
    protected void onLoad() {
        super.onLoad();
        setWidth("255px");
        setHeight("250px");
        setGlassEnabled(true);
        setModal(true);
        countEdit.setValue(1);
        countEdit.setMaxLength(3);
        countEdit.addKeyPressHandler(new KeyPressHandler() {

            @Override
            public void onKeyPress(KeyPressEvent event) {
                int keyCode = event.getNativeEvent().getKeyCode();
                if ((!Character.isDigit(event.getCharCode()))
                        && (keyCode != KeyCodes.KEY_TAB)
                        && (keyCode != KeyCodes.KEY_BACKSPACE)
                        && (keyCode != KeyCodes.KEY_DELETE)
                        && (keyCode != KeyCodes.KEY_HOME)
                        && (keyCode != KeyCodes.KEY_END)
                        && (keyCode != KeyCodes.KEY_LEFT)
                        && (keyCode != KeyCodes.KEY_UP)
                        && (keyCode != KeyCodes.KEY_RIGHT)
                        && (keyCode != KeyCodes.KEY_DOWN)) {
                    ((IntegerBox) event.getSource()).cancelKey();
                }
            }
        });
        center();
    }

    @UiHandler("cancelSale")
    public void onClickCancel(ClickEvent event) {
        hide();
    }

    @UiHandler("saveSale")
    public void onClickSave(ClickEvent event) {
        DateTimeFormat fomeFormat = DateTimeFormat.getFormat("dd/MM/yyyy HH:mm:ss");
        List<Map> productList = new ArrayList<>();
        Map itemSales = new LinkedHashMap();
        itemSales.put("ID_PRODUCT", bean.getProduct().toString());
        itemSales.put("COUNT", countEdit.getText());
        productList.add(itemSales);
        final Map params = new LinkedHashMap();
        params.put("SEQ", 0);
        params.put("DATE", fomeFormat.format(new Date(System.currentTimeMillis())));
        params.put("NR_SEQ_PF", null);
        params.put("PRODUCTS", productList);

        hanCadSOService.insertNewItem(params, new HannaRequestCallback() {

            @Override
            public void onResponseReceived(Request request, Response response) {
                LogBrowser.writeConsole(response.getText(), null);
                hide();
                LogBrowser.alert("Solicitação realizado com sucesso!");
            }
        });
    }

    public void show(HanCadSAPresenter.Display view, ProductBean bean) {
        this.bean = bean;
        minStock.setText("" + bean.getMinStock());
        show();
    }

    interface NewRequestDialogUiBinder extends UiBinder<Widget, NewRequestDialog> {
    }
}
